package copyengine.ai.pathFinder.astart.parentTile
{
	import copyengine.ai.pathFinder.astart.node.CEAStartNodeBasic;
	import copyengine.utils.debug.CELog;

	public final class CEAStartSearchParentNode
	{
		//		//当前的Search版本,因为所以Tile节点不会重复New,所以用该变量确定当前节点是否在
		//		//这次Search中使用
		//		public var currentSearchVersion:int

		//Search结果链表
		public var parentNode:CEAStartSearchParentNode;

		//每块Tile是通过内部的Node链接在一起的
		//通过这个UUID可以找到对应的是哪个Node
		public var nodeLinkUUID:String;

		private var subNodeVector:Vector.<CEAStartNodeBasic>;

		//标记当前是否为Close节点
		public var isCloseNode:Boolean;

		//标记当前是否为Open节点
		public var isOpenNode:Boolean;

		//当前节点的启发成本
		public var heuristicCost:int;

		//从搜索点到达改点的移动成本(步长成本均为1即可)
		public var costFromStart:int;

		//计算成本的总和
		public var totalCost:int;

		public var tileIndexX:int;
		public var tileIndexY:int;

		public function CEAStartSearchParentNode()
		{
		}

		public function initialize(_tileIndexX:int, _tileIndexY:int):void
		{
			tileIndexX = _tileIndexX;
			tileIndexY = _tileIndexY;
			subNodeVector = new Vector.<CEAStartNodeBasic>();
		}

		public function dispose():void
		{
			subNodeVector = null;
		}

		public function addSubNode(_node:CEAStartNodeBasic):void
		{
			subNodeVector.push(_node);
		}

		public function getAllSubNode():Vector.<CEAStartNodeBasic>
		{
			return subNodeVector;
		}

		/**
		 *当一次AStart探索结束后重置AStart搜索的全部数值
		 */
		public function resetParentNodeAStartProperty():void
		{
			parentNode = null;
			isCloseNode = false;
			isOpenNode = false;
			totalCost = 0;
			costFromStart = 0;
			heuristicCost = int.MIN_VALUE;
		}

		/**
		 *注意!
		 * 如果指定UUID则使用UUID匹配唯一Node
		 * 如果不指定 调用该函数时候上层需要能确保当前节点下每个Type类型只有一个Node
		 * 否则只能取到第一个Node节点
		 */
		public function getSubNode(_subNodeUUID:String = null, _type:int = int.MIN_VALUE):CEAStartNodeBasic
		{
			for each (var subNode:CEAStartNodeBasic in subNodeVector)
			{
				if (_subNodeUUID != null)
				{
					if (subNode.getUUID() == _subNodeUUID)
					{
						return subNode;
					}
				}
				else
				{
					if (subNode.getNodeType() == _type)
					{
						return subNode;
					}
				}
			}
			CELog.err("can not's find subNode by type : " + _type + " uuid : " + _subNodeUUID);
			return null;
		}

		public function getIndexX():int
		{
			return tileIndexX;
		}

		public function getIndexY():int
		{
			return tileIndexY;
		}

		public function toString():String
		{
			return "CEAStartSearchParentNode ( " + tileIndexX + ", " + tileIndexY + ")";
		}


	}
}
